package com.poobo.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.MoveAction;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.poobo.aop.SystemCtrLo4jAop;
import com.poobo.aop.SystemServiceLo4jAop;
import com.poobo.constants.WebConstants;
import com.poobo.entity.TbSysAccount;
import com.poobo.service.ISysAccountService;
import com.poobo.util.message.AjaxResult;
import com.poobo.util.pager.Page;
import com.poobo.util.str.StrUtil;


@Controller
@RequestMapping("user")
public class SysAccountCtr {

	@Autowired
	private ISysAccountService iSysAccountService;
	private AjaxResult ajaxResult;
	private List<TbSysAccount> _sysAccount;
	private ModelAndView view;
	private Logger log = Logger.getLogger(SysAccountCtr.class);
	
	/**
	 * 系统用户登录
	 * @param request
	 * @param response
	 * @param loginName
	 * @param loginPwd
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="login/{loginName}/{loginPwd}/{encodeURI}",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String ajaxLogin(HttpServletRequest request,HttpServletResponse response,@PathVariable String loginName,@PathVariable String loginPwd) throws IOException{
		ajaxResult=new AjaxResult();
		if(StrUtil.isNotNull(loginPwd) && StrUtil.isNotNull(loginName)){
			_sysAccount=iSysAccountService.getSysTbAccount(loginName, loginPwd);
			if(_sysAccount!=null && _sysAccount.size()>0){
				if(_sysAccount.get(0).getLoginPwd().equals(loginPwd)){
					log.info(_sysAccount.get(0).getLoginName()+"成功登陆!");
					request.getSession().setAttribute(WebConstants.CURRENT_USER, _sysAccount.get(0));
					ajaxResult.setStatus(true);
					ajaxResult.setCallback("init/index.html");
				}else{
					ajaxResult.setStatus(false);
					ajaxResult.setMessage("密码错误!");
				}
			}else{
				ajaxResult.setStatus(false);
				ajaxResult.setMessage("账号或密码错误!");
			}
		}else{
			ajaxResult.setStatus(false);
			ajaxResult.setMessage("参数错误!");
		}
		return JSON.toJSONString(ajaxResult);
	}
	
	/**
	 * 系统用户登出
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("toLoginOut")
	@SystemCtrLo4jAop(description="用户退出")
	public String toLoginOut(HttpServletRequest request,HttpServletResponse response){
		TbSysAccount tbSysAccount=(TbSysAccount) request.getSession().getAttribute(WebConstants.CURRENT_USER);
		if(tbSysAccount!=null){
			request.getSession().removeAttribute(WebConstants.CURRENT_USER);
			log.info("用户："+tbSysAccount.getName()+"退出成功");
		}else{
			System.out.println("当前没有登录");
		}
		return "forward:/init/login.html";
	}
	
	/**
	 * 查询所有系统用户(含分页)
	 * @param request
	 * @param tbSysAccount
	 * @param page
	 * @return
	 */
	@RequestMapping("getSysList")
	public ModelAndView getSysList(HttpServletRequest request,TbSysAccount tbSysAccount,Page page){
		view=new ModelAndView();
		Page sysAcountPager =iSysAccountService.findTbAccount(tbSysAccount,page);
		view.addObject("sysAcountPager",sysAcountPager.getPageList());
		view.addObject("pageSize", page.getPageSize());
		view.addObject("rowCount", page.getRowCount());
		view.addObject("pageNo", page.getPageNo());
		view.addObject("pageNum", page.getPageNum());
		view.setViewName("sysAccount/sysList");
		return view;
	}
	/**
	 * 跳转修改密码页面
	 * @param request
	 * @return
	 */
	@RequestMapping("toUpdateLoginPwdPage")
	public String toUpdateLoginPwd( HttpServletRequest request ){
//		Integer userId = Integer.parseInt(request.getParameter("userId"));
//		request.setAttribute("userId", userId);
		return "sysAccount/update_password";
	}
	/**
	 * 修改密码
	 * @param request
	 * @param sysAccount
	 * @return
	 */
	@RequestMapping("updateLoginPwd")
	@ResponseBody
	public String updateLoginPwd( HttpServletRequest request, TbSysAccount sysAccount ){
		Integer userId = Integer.parseInt(request.getParameter("userId"));
		String oldLoginPwd = request.getParameter("oldLoginPwd");
		String newLoginPwd = request.getParameter("newLoginPwd");
		sysAccount = iSysAccountService.finSysAccountById(userId);
		if( sysAccount!=null && oldLoginPwd.equals(sysAccount.getLoginPwd()) ){
			sysAccount.setLoginPwd(newLoginPwd);
			iSysAccountService.updateSysAccount(sysAccount);
			return "success";
		}else{
			return "fail";
		}
	}
	
}
